package com.itany.corejava.code06_面向对象.bms;

import java.util.Scanner;

/**
 * @author MissChen
 * @date 2024年02月26日 14:09
 */
public class AccountMenu {
    private Scanner sc = new Scanner(System.in);
    private Account account;

    public AccountMenu(Account account) {
        this.account = account;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public void showMainMenu() {
        System.out.println("****************欢迎进入银行系统主菜单*****************");
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款金额 8.修改密码 9.注销)");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                deposit();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                providerLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                logout();
                break;

        }

    }

    //存款方法
    public void deposit() {
        System.out.println("************开始存款***********");
        System.out.print("请输入存款的金额:");
        double money = sc.nextDouble();

        //调用存款方法
        account.deposit(money);
        System.out.println("提示:存款成功!");
        showMainMenu();

    }

    //取款方法
    public void withdraw() {
        System.out.println("*************开始取款****************");
        System.out.print("请输入取款的金额:");
        double money = sc.nextDouble();

        //调用取款方法
        boolean flag = account.withdrawMoney(money);
        if(flag){
            System.out.println("提示:取款成功!");
        }else{
            System.out.println("错误:余额不足，取款失败!");
        }
        showMainMenu();
    }

    //余额查询方法
    public void queryBalance() {
        System.out.println("****************查询余额*****************");
        System.out.println("您当前账户余额为：" + account.getBalance() + "元");
        showMainMenu();
    }

    //转账
    public void transfer(){
        System.out.println("*************开始转账***************");
        System.out.println("请输入要转入的目标账户的编号");
        int id=sc.nextInt();

        //查询目标账户是否存在
        Account targetAccount=Bank.findById(id);
        //如果返回值为null，说明对应账户不存在
        if(targetAccount==null){
            System.out.println("错误:目标账户不存在");
        }else{
            //如果目标账户存在，则执行转账操作
            System.out.print("请输入转账的金额:");
            double money=sc.nextDouble();
            if(account.getBalance()>=money){
                //当前账户余额减少
                account.setBalance(account.getBalance()-money);

                //目标账户余额增加
                targetAccount.setBalance(targetAccount.getBalance()+money);
                System.out.println("提示:恭喜您，转账成功,您当前转账余额为:"+account.getBalance());
            }else{
                System.out.println("错误:对不起，您转账失败，账户余额不足，您当前余额为:"+account.getBalance());
            }
        }
        showMainMenu();
    }

    //贷款
    public void providerLoan(){
        System.out.println("************开始贷款**************");
        // Loanable loan=new LoanSavingAccount();
        // Loanable loan2=new LoanCreditAccount();

        // 所有的贷款账户都实现了Loanable接口
        // 此时，可以直接通过接口来进行判断
        // 能够转换成对应接口的类一定是该接口对应的两个实现类之一
        if(account instanceof Loanable){
            System.out.print("请输入贷款金额:");
            double money=sc.nextDouble();

            //贷款功能只有Loanable接口对应的实现类提供
            Loanable loanable= (Loanable) account;

            //执行贷款方法
            loanable.providerLoan(money);
            System.out.println("提示:恭喜您，贷款成功，您当前的贷款金额为:"+loanable.queryLoan());

        }else{
            System.out.println("错误:对不起，您的账户不是贷款账户，无法操作贷款功能");
        }
        showMainMenu();

    }

    //还款
    public void payLoan(){
        System.out.println("*************开始还贷***************");
        //只有贷款账户才能执行贷款方法
        if(account instanceof  Loanable){
            Loanable loanable=(Loanable) account;
            System.out.print("请输入您需要还贷的金额:");
            double money=sc.nextDouble();
            boolean flag=loanable.payLoan(money);
            if(flag){
                System.out.println("提示:恭喜您，还贷成功，当前账户余额:"+account.getBalance()+",当前贷款总额:"+loanable.queryLoan());
            }else{
                System.out.println("错误:对不起，您的账户余额不足，还贷失败！您的账户余额:"+account.getBalance());
            }
        }else{
            System.out.println("错误:对不起，您的账户不是贷款账户，无法操作还贷款功能");
        }
        showMainMenu();
    }

    //查询贷款金额
    public void queryLoan(){
        System.out.println("***************查询贷款总额****************");
        if(account instanceof Loanable){
            Loanable loanable= (Loanable) account;
            System.out.println("提示:您当前贷款总额为:"+loanable.queryLoan());
        }else{
            System.out.println("错误:对不起，您的账户不是贷款账户，无法操作查询贷款功能");
        }
        showMainMenu();
    }

    public void modifyPassword(){
        System.out.println("*************密码修改*************");
        System.out.print("请输入原密码:");
        String oldPassword=sc.next();
        if(oldPassword.equals(account.getPassword())){
            //如果原密码输入正确，则执行修改密码操作
            System.out.print("请输入新密码:");
            String newPassword=sc.next();
            System.out.println("请再次输入新的密码:");
            String newPassword2=sc.next();
            //如果两次密码输入一致，则进行修改
            if(newPassword.equals(newPassword2)){
                account.setPassword(newPassword);
                System.out.println("提示:密码修改成功，请重新登录");
                new Start().showMenu();
            }else{
                System.out.println("两次输入的密码不一致，请重新输入");
            }
        }else{
            System.out.println("错误:您输入的原密码不正确");
        }
        showMainMenu();
    }

    //注销
    public void logout(){
        System.out.println("提示:注销成功");
        new Start().showMenu();
    }


}
